package umich.max.geolocation.featextract.ner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import max.nlp.dal.blog.blogger.BloggerBlog;
import max.nlp.dal.blog.blogger.BloggerDB;
import max.nlp.dal.blog.blogger.BloggerPost;
import max.nlp.wrappers.ml.weka.WekaFeatureExtractor;
import umich.max.geolocation.util.BlogUtils;

import com.mongodb.DBCursor;

public class LocationCounterExtractor extends WekaFeatureExtractor<String, Integer> {



	private static String prefix = "iLOC_";

	private BloggerDB db = BloggerDB.getInstance();

	
	public LocationCounterExtractor() {
		super(prefix);
	}
	
	public List<HashMap<String, Integer>> extractFeaturesForClass(
			String className) {
		ArrayList<HashMap<String, Integer>> locVectors = new ArrayList<HashMap<String, Integer>>();
		DBCursor itr = db.getBlogIterator();

		while (itr.hasNext()) {
			BloggerBlog blog = new BloggerBlog(itr.next());
			if (BlogUtils.isBlogFromState(blog, className)) {
				locVectors.add(extractFeaturesForObject(blog));
			}
		}
		return locVectors;
	}

	@Override
	public HashMap<String, Integer> extractFeaturesForObject(Object item) {
		BloggerBlog blog = (BloggerBlog) item;
		HashMap<String, Integer> locsInBlog = new HashMap<String, Integer>();

		for (BloggerPost post : blog.getPosts()) {
			List<String> locations = post.getLocations();
			if (!locations.isEmpty()) {

				for (String location : locations) {
					location = prefix + location;
					Integer count = locsInBlog.get(location);
					if (count == null)
						locsInBlog.put(location, 1);
					else
						locsInBlog.put(location, count + 1);
				}
			}
		}
		return locsInBlog;
	}

}
